package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService service;

    @RequestMapping("")
    @PermissionName("员工列表")
    @RequiresPermissions("department:list")
    public String index(){
        return "employee/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @PermissionName("员工数据")
    @RequiresPermissions("employee:data")
    public PageResult list(EmployeeQueryObject qo){
        return service.query(qo);
    }
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("员工保存")
    @RequiresPermissions("employee:list")
    public JsonResult save(Employee entity){
        JsonResult result = new JsonResult();
        try {
            service.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("员工更新")
    @RequiresPermissions("employee:update")
    public JsonResult update(Employee entity){
        JsonResult result = new JsonResult();
        try {
            service.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑操作失败");
        }
        return result;
    }
    @RequestMapping("changeState")
    @ResponseBody
    @PermissionName("员工状态")
    @RequiresPermissions("employee:changeState")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            service.changeState(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑操作失败");
        }
        return result;
    }

    @RequestMapping("validateUsername")
    @ResponseBody
    @PermissionName("员工检验")
    @RequiresPermissions("employee:checkName")
    public boolean validateUsername(String username,Long id){
        Employee employee =  service.checkName(username);
        if(id == null){
            return employee==null;
        }else{
            return true;
        }

    }
}
